package com.pt.service.impl;

import com.pt.dto.response.UserSettingsResponse;
import com.pt.service.NotificationService;
import com.pt.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationServiceImpl implements NotificationService {
    
    private final UserService userService;
    
    @Override
    public void sendEmailNotification(Long userId, String subject, String content) {
        try {
            // 注意：这里应该获取目标用户的设置，但目前getUserSettings()没有传递userId参数
            // 临时解决方案：直接发送通知（实际项目中需要修改getUserSettings方法）
            log.info("=== 邮件通知 ===");
            log.info("收件人ID: {}", userId);
            log.info("主题: {}", subject);
            log.info("内容: {}", content);
            log.info("邮件发送成功（模拟）");
        } catch (Exception e) {
            log.error("发送邮件通知失败: {}", e.getMessage(), e);
        }
    }
    
    private void sendEmailNotificationOld(Long userId, String subject, String content) {
        try {
            UserSettingsResponse settings = userService.getUserSettings();
            if (settings.getNotifications().getEmailNotifications()) {
                // 模拟发送邮件
                log.info("=== 邮件通知 ===");
                log.info("收件人ID: {}", userId);
                log.info("主题: {}", subject);
                log.info("内容: {}", content);
                log.info("邮件发送成功（模拟）");
            } else {
                log.info("用户{}已关闭邮件通知", userId);
            }
        } catch (Exception e) {
            log.error("发送邮件通知失败: {}", e.getMessage(), e);
        }
    }
    
    @Override
    public void sendTorrentNotification(Long userId, String torrentTitle, String action) {
        try {
            String content = String.format("种子【%s】%s", torrentTitle, action);
            log.info("=== 种子通知 ===");
            log.info("用户ID: {}", userId);
            log.info("内容: {}", content);
            log.info("种子通知发送成功（模拟）");
            
            // 同时发送邮件通知
            sendEmailNotification(userId, "种子通知", content);
        } catch (Exception e) {
            log.error("发送种子通知失败: {}", e.getMessage(), e);
        }
    }
    
    @Override
    public void sendMessageNotification(Long userId, String senderName, String subject) {
        try {
            String content = String.format("您收到来自【%s】的新私信：%s", senderName, subject);
            log.info("=== 私信通知 ===");
            log.info("收件人ID: {}", userId);
            log.info("发件人: {}", senderName);
            log.info("主题: {}", subject);
            log.info("私信通知发送成功（模拟）");
            
            // 同时发送邮件通知
            sendEmailNotification(userId, "私信通知", content);
        } catch (Exception e) {
            log.error("发送私信通知失败: {}", e.getMessage(), e);
        }
    }
    
    @Override
    public void sendSystemNotification(Long userId, String title, String content) {
        try {
            log.info("=== 系统通知 ===");
            log.info("用户ID: {}", userId);
            log.info("标题: {}", title);
            log.info("内容: {}", content);
            log.info("系统通知发送成功（模拟）");
            
            // 同时发送邮件通知
            sendEmailNotification(userId, "系统通知：" + title, content);
        } catch (Exception e) {
            log.error("发送系统通知失败: {}", e.getMessage(), e);
        }
    }
} 